package com.njtech.smartuniversity.controller;

import com.github.pagehelper.PageInfo;
import com.njtech.smartuniversity.bean.CaseBean;
import com.njtech.smartuniversity.service.CaseService;
import com.njtech.smartuniversity.util.JsonResult;
import com.njtech.smartuniversity.util.JsonUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Date;

/**
 * Created by ritchie on 6/25/18
 */

@RestController
@Api(value = "/api/case", tags = {"案件"}, description = "案件相关")
@RequestMapping("/api/case")
public class CaseController {

    @Autowired
    CaseService mCaseService;


//    @GetMapping("/{id}")
//    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_CUSTOMER') or hasRole('ROLE_MERCHANT')")
//    @ApiOperation(value = "单个用户查询接口", notes = "使用ID查询用户")
//    @ApiImplicitParams({
//            @ApiImplicitParam(value = "用户ID(学号/工号)", name = "id", dataType = "String", paramType = "path", required = true)
//    })
//    @ApiResponses({
//            @ApiResponse(code = 400, message = "请求参数有误"),
//            @ApiResponse(code = 401, message = "未授权"),
//            @ApiResponse(code = 403, message = "禁止访问"),
//            @ApiResponse(code = 404, message = "请求路径不存在"),
//            @ApiResponse(code = 500, message = "服务器内部错误")
//    })
//    public JsonResult GetUser(@PathVariable("id") String id) {
//        UserBean user = mUserService.selectByUserId(id);
//        if (user != null) {
//            return new JsonUtil().successDataDefault(user);
//        }
//
//        return new JsonUtil().wrongNoDataWithMessage("不存在该地域");
//    }

    @GetMapping("/list/{area_id}")
    @PreAuthorize("hasRole('ROLE_MERCHANT')")
    @ApiOperation(value = "案件列表接口", notes = "获取所有案件信息")
    @ApiImplicitParams({

            @ApiImplicitParam(value = "用户地域", name = "area_id", dataType = "int", paramType = "path")
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public JsonResult ListCases(
            @PathVariable(value = "area_id") int area_id
    ) {
        if(area_id==5){

        }
        List<CaseBean> areaBeanList = mCaseService.getAllCase(area_id);
        if (areaBeanList != null) {
            return new JsonUtil().successDataDefault(areaBeanList);
        }
        return new JsonUtil().wrongNoDataWithMessage("不存在案件信息");

    }


    @GetMapping("/{area_id}")
    @PreAuthorize("hasRole('ROLE_MERCHANT')")
    @ApiOperation(value = "案件列表分页接口", notes = "获取分页案件信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "page", dataType = "int", paramType = "form"),
            @ApiImplicitParam(value = "每页数量", name = "size", dataType = "int", paramType = "form"),
            @ApiImplicitParam(value = "用户地域", name = "area_id", dataType = "int", paramType = "path")
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public JsonResult ListPageUsers(@RequestParam(value = "page", defaultValue = "1") int pageNum,
                                    @RequestParam(value = "size", defaultValue = "10") int pageSize,
                                    @PathVariable(value = "area_id") int area_id) {
        if(area_id==5){
            area_id = 0;
        }
        PageInfo<CaseBean> pageInfo = mCaseService.getCasesByPage(pageNum, pageSize,area_id);
        if (pageInfo != null) {
            return new JsonUtil().successDataDefault(pageInfo);
        }
        return new JsonUtil().wrongNoDataWithMessage("不存在地域信息");

    }

    @DeleteMapping(value = "del/{id}")
    @PreAuthorize("hasRole('ROLE_MERCHANT')")
    @ApiOperation(value = "案件删除接口", notes = "案件指定ID的用户")
    @ApiImplicitParam(value = "案件ID", name = "id", dataType = "int", paramType = "path", required = true)
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public JsonResult DeleteCase(@PathVariable("id") int id) {
        boolean result = mCaseService.deleteById(id);
        if (result) {

            return new JsonUtil().successEmptyDataDefault("删除该案件成功");

        }

        return new JsonUtil().wrongNoDataWithMessage("不存在该案件");

    }


    @PostMapping(value = "update/{id}")
    @PreAuthorize("hasRole('ROLE_MERCHANT')")
    @CrossOrigin
    @ApiOperation(value = "案件信息修改接口", notes = "修改指定ID的案件信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "案件ID", name = "id", dataType = "Integer", paramType = "path", required = true),
            @ApiImplicitParam(value = "案件名", name = "casename", dataType = "String", paramType = "form"),
            @ApiImplicitParam(value = "地域id", name = "area_id", dataType = "Integer", paramType = "form"),
            @ApiImplicitParam(value = "案件编号", name = "numbers", dataType = "String", paramType = "form"),
            @ApiImplicitParam(value = "案件类别", name = "casetype_id", dataType = "Integer", paramType = "form"),
            @ApiImplicitParam(value = "图片地址 ", name = "picpath", dataType = "String", paramType = "form"),
            @ApiImplicitParam(value = "罪名类型", name = "crimetypes", dataType = "String", paramType = "form"),
            @ApiImplicitParam(value = "基本情况", name = "detail", dataType = "String", paramType = "form"),
            @ApiImplicitParam(value = "文书号", name = "writid", dataType = "String", paramType = "form"),
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public JsonResult UpdateCase(@PathVariable("id") int id,
                                 @RequestParam(value = "casename") String casename,
                                 @RequestParam(value = "area_id") int area_id,
                                 @RequestParam(value = "numbers") String numbers,
                                 @RequestParam(value = "casetype_id") int casetype_id,
                                 @RequestParam(value = "crimetypes") String intendeds,
                                 @RequestParam(value = "picpath") String picpath,
                                 @RequestParam(value = "writid") String writid,
                                 @RequestParam(value = "detail") String basic

    ){
        CaseBean caseBean= new CaseBean();
        caseBean.setCasename(casename);
        caseBean.setArea_id(area_id);
        caseBean.setNumbers(numbers);
        caseBean.setCasetype_id(casetype_id);
        caseBean.setIntendeds(intendeds);
        caseBean.setPicpath(picpath);
        caseBean.setBasic(basic);
        caseBean.setWritid(writid);
        caseBean.setId(id);
        caseBean.setStlye(1);
        if(casetype_id == 3){
            caseBean.setResources("1,2,3");
        }else if(casetype_id == 4){
            caseBean.setResources("1,2,3");
        }else{
            caseBean.setResources("1,2,3,4,5,6");
        }


        return new JsonUtil().successDataWithMessage(mCaseService.update(caseBean), "修改案件信息成功");
    }

    @PostMapping("add")
    @PreAuthorize("hasRole('ROLE_MERCHANT')")
    @ApiOperation(value = "案件信息添加接口", notes = "添加指定ID的案件信息")
    @CrossOrigin
    @ApiImplicitParams({
            @ApiImplicitParam(value = "案件名", name = "casename", dataType = "String", paramType = "form"),
            @ApiImplicitParam(value = "地域id", name = "area_id", dataType = "Integer", paramType = "form"),
            @ApiImplicitParam(value = "案件编号", name = "numbers", dataType = "String", paramType = "form"),
            @ApiImplicitParam(value = "案件类别", name = "casetype_id", dataType = "Integer", paramType = "form"),
            @ApiImplicitParam(value = "图片地址 ", name = "picpath", dataType = "String", paramType = "form"),
            @ApiImplicitParam(value = "罪名类型", name = "crimetypes", dataType = "String", paramType = "form"),
            @ApiImplicitParam(value = "基本情况", name = "detail", dataType = "String", paramType = "form"),
            @ApiImplicitParam(value = "文书号", name = "writid", dataType = "String", paramType = "form"),
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public JsonResult addCase(@RequestParam(value = "casename") String casename,
                              @RequestParam(value = "area_id") int area_id,
                              @RequestParam(value = "numbers") String numbers,
                              @RequestParam(value = "casetype_id") int casetype_id,
                              @RequestParam(value = "crimetypes") String intendeds,
                              @RequestParam(value = "picpath") String picpath,
                              @RequestParam(value = "writid") String writid,
                              @RequestParam(value = "detail") String basic

    ){
        CaseBean caseBean= new CaseBean();
        caseBean.setCasename(casename);
        caseBean.setArea_id(area_id);
        caseBean.setNumbers(numbers);
        caseBean.setCasetype_id(casetype_id);
        caseBean.setIntendeds(intendeds);
        caseBean.setPicpath(picpath);
        caseBean.setBasic(basic);
        caseBean.setWritid(writid);
        caseBean.setNewtime(new Date());
        if(casetype_id == 3){
            caseBean.setResources("1,2,3");
        }else if(casetype_id == 4){
            caseBean.setResources("1,2,3");
        }else{
            caseBean.setResources("1,2,3,4,5,6");
        }


        return new JsonUtil().successDataWithMessage(mCaseService.addCase(caseBean), "添加案件信息成功");
    }

    @GetMapping("selcet/{id}")
    @PreAuthorize("hasRole('ROLE_MERCHANT')")
    @ApiOperation(value = "案件信息接口", notes = "案件详情")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "案件id", name = "id", dataType = "int", paramType = "path", required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })

    public JsonResult GetCase(@PathVariable("id") int id) {
        CaseBean goodsBean = mCaseService.select(id);
        if (goodsBean != null) {
            return new JsonUtil().successDataDefault(goodsBean);
        }
        return new JsonUtil().wrongNoDataWithMessage("不存在该案件");
    }


}
